require(['./config'], () => {
    require(['swiper', 'template', 'request','jquery'], (Swiper, tmp,req) => {
        class Index {
            constructor() {
                this.count = 0
                this.initSwiper()
                this.getSugarData()
                this.getSugarPeiJ()
                this.getNavData()
                this.gwc()
            }
            // 初始化轮播图
            initSwiper() {
                new Swiper('.swiper-container', {
                    pagination: '.swiper-pagination',
                    nextButton: '.swiper-button-next',
                    prevButton: '.swiper-button-prev',
                    slidesPerView: 1,
                    paginationClickable: true,
                    spaceBetween: 30,
                    loop: true,
                    centeredSlides: true,
                    autoplay: 4000,
                    autoplayDisableOnInteraction: false,
                })
            }
            // 获取糖猫产品数据
            getSugarData() {
                req.getIndexSugar1()
                    .then(res => {
                        // console.log(res)
                        if (res.code === 1) {
                            // 渲染页面
                            this.renderSugar(res.result_body)
                        }
                    })
            }
            // 渲染糖猫产品页面
            renderSugar(res) {
                // console.log(res)
                const html = tmp('templateSugar', { list: res })
                // console.log(html)
                $('.sugarDd').html(html)
            }
            getSugarPeiJ(){
                req.getIndexSugar2()
                .then(res=>{
                    if(res.code === 1){
                        this.renderSugarPeiJ(res.result)
                    }
                })
            }
            renderSugarPeiJ(res){
                console.log(res)
            }
            renderSugarPeiJ(res) {
                // console.log(res)
                const html = tmp('templateSugarPeiJ', { list: res })
                // console.log(html)
                $('.sugarDr').html(html)
            }
            getNavData(){
                req.getNav()
                .then(res =>{
                    if(res.code === 200){
                        this.render(res.data.list)
                    }
                })
            }
            render(res){
                // console.log(res)
                // console.log($('.con ul'))
                const list = res.splice(1,7)
                const html = tmp('templateNav',{list})
                $('.con ul').html(html)
            }
            gwc(){
                let prodList = localStorage.getItem('prodList')
                if(prodList){
                    prodList = JSON.parse(prodList)
                    prodList.forEach(item =>{
                        this.count += item.count
                    })
                }else{}
               $('.gwc').text(this.count)
            }
        }
        return new Index()
        })
    })
// const reg = document.querySelectorAll('.content ul li')
// for (i = 0; i < reg.length; i++) {
//     reg[i].onclick = ()=>{
//         window.location.href = './src/html/Details.html'
//     }
// }
// const lii = document.querySelectorAll('.searchbar ul li')[0]
// const uls = document.querySelectorAll('.searchbar ul li ul')[0]
// const lii1 = document.querySelectorAll('.searchbar ul .lii1')[0]
// const uls1 = document.querySelectorAll('.searchbar ul .lii1 ul')[0]
// // console.log(lii1,uls1);
// lii.onmouseenter = ()=>{
//     css(uls, 'display','block')
// }
// lii.onmouseleave = ()=>{
//     css(uls,'display','none')
// }
// lii1.onmouseenter = ()=>{
//     css(uls1, 'display','block')
// }
// lii1.onmouseleave = ()=>{
//     css(uls1,'display','none')
// }